function DownloadPDFView(calendarApplication) {
    this._calendarApplication = calendarApplication;
    this.openReportDialogButton = $('#open_report_dialog_button');
    this.downloadReportDialog = $('#download_report_dialog');
    this.downloadButton= $("#report_pdf_submit");
    this.cancelButton= $("#report_cancel");
    this.serilizedReportHiddenElement= $('#report-pdf-form #report');
    this.reportForm= $('#report-pdf-form');
    this._errorElement = $('#download_report_errors');
    this._rangeType = this.reportForm.find('.range-type');

    var yearMonthItemBase = {
        setDate : function(year, month) {
            this.yearDom.val(year);
            this.monthDom.val(month);
        },

        getDate : function() {
            return Date.parseExact(this.yearDom.val().trim() + '/' + this.monthDom.val().trim() , 'yyyy/M');
        },

        enable: function(){
            this.yearDom.removeClass('disabled');
            this.monthDom.removeClass('disabled');
            this.yearDom.removeAttr('readonly');
            this.monthDom.removeAttr('readonly');
            this.monthDom.removeAttr('disabled');
        },

        disable: function(){
            this.yearDom.addClass('disabled');
            this.monthDom.addClass('disabled');
            this.yearDom.attr('readonly', 'readonly');
            this.monthDom.attr('readonly', 'readonly');
            this.monthDom.attr('disabled', 'disabled');
        },

        addError : function(errorHolder, msg) {
            errorHolder.addError(msg, this.yearDom.attr('id'));
            errorHolder.addError(msg, this.monthDom.attr('id'));
        },

        validate: function(errorHolder, msg) {
            var month = this.monthDom.val().trim();
            if (parseInt(month) == 0 || this.getDate() == null) {
                this.addError(errorHolder, msg);
            }
        }
    };

    this.startYearMonth = createYearMonthItem({yearDom:$('#report_start_year') , monthDom:$('#report_start_month')});
    this.endYearMonth = createYearMonthItem({yearDom:$('#report_end_year') , monthDom:$('#report_end_month')});
    function createYearMonthItem(item) {
        $.extend(item, yearMonthItemBase);
        return item;
    }
}

DownloadPDFView.prototype = {
    init: function() {
        this._createDialog();
        this._bindDownloadDialogEvent();
    },

    _createDialog: function(){
        this.downloadReportDialog.dialog({
            dialogClass: 'download-report-dialog',
            bgiframe: true,
            title: i18n('LABEL_DOWNLOAD_CALENDAR_PDF'),
            width: 400,
            autoOpen: false,
            modal: true,
            closeOnEscape: true
        });
    },

    _validateInput: function(){
        var error = new Error();
        if (this.getDownloadRangeType() == 'editRange'){
            var startItem = this.startYearMonth;
            var endItem = this.endYearMonth;

            startItem.validate(error, i18n('ERROR_START_DATE_IS_INVALID'));
            endItem.validate(error, i18n('ERROR_END_DATE_IS_INVALID'));

            if (error.hasError()){
                return error;
            }

            if(startItem.getDate() > endItem.getDate()){
                startItem.addError(error, i18n('ERROR_START_DATE_SHOULD_EARLIER_THAN_END_DATE'));
                endItem.addError(error, i18n('ERROR_START_DATE_SHOULD_EARLIER_THAN_END_DATE'));
            }

            if((endItem.getDate() - startItem.getDate()) / 3600000 / 24 > 365 * 5){
                startItem.addError(error, i18n('ERROR_RANGE_SHOULD_NOT_EXCEED_MAX_VALUE'));
                endItem.addError(error, i18n('ERROR_RANGE_SHOULD_NOT_EXCEED_MAX_VALUE'));
            }
        }
        return error;
    },

    _clearErrors: function() {
        this._errorElement.find('p.error').remove();
        this.reportForm.find('.error').removeClass('error');
    },

    _renderErrors: function(error){
        this._clearErrors();
        error.errors.foreach(function(aError) {
            var fieldView = $('#' + aError.errorField);
            fieldView.addClass('error');
        });
        this._errorElement.append('<p class="error">' + error.messages().join('<br />') + '</p>');
    },

    _updateRange: function() {
        if (this.getDownloadRangeType() == 'editRange'){
            this.startYearMonth.enable();
            this.endYearMonth.enable();
        }
        else {
            this.startYearMonth.disable();
            this.endYearMonth.disable();
        }
    },

    _updateDialog: function(){
        var dateRange = this._getHeaderDateRangeCallBack();
        var startYearMonth = YearMonth.fromDate(dateRange.start);
        var endYearMonth = YearMonth.fromDate(dateRange.end);
        this.startYearMonth.setDate(startYearMonth.year(), startYearMonth.month());
        this.endYearMonth.setDate(endYearMonth.year(), endYearMonth.month());
        this._updateRange();
    },

    _bindDownloadDialogEvent: function() {
        var me = this;

        this._rangeType.change(function(){
            me._updateRange();
        });
        
        this.openReportDialogButton.click(function(){
            me._clearErrors();
            me._updateDialog();
            me.downloadReportDialog.dialog('open');
        });

        this.downloadButton.click(function() {
            var error = me._validateInput();
            if (error.hasError()){
                me._renderErrors(error);
                return;
            }
            me.serilizedReportHiddenElement.val(me._getSerizlizedReportCallback());
            me.reportForm.submit();
            me._clearErrors();
        });

        this.cancelButton.click(function() {
            me.downloadReportDialog.dialog('close');
        });
    },

    getDownloadRangeType: function() {
        return this._rangeType.find(':radio[name=rangeType]:checked').val();
    },

    getReportRange: function(){
        var startYearMonth = YearMonth.fromDate(this.startYearMonth.getDate());
        var endYearMonth = YearMonth.fromDate(this.endYearMonth.getDate());
        return new DateRange(startYearMonth.firstDayOfMonth(), endYearMonth.lastDayOfMonth());
    },

    onGetSerizlizedReport: function(callback) {
        this._getSerizlizedReportCallback = callback;
    },

    onGetHeaderDateRange: function(callback) {
        this._getHeaderDateRangeCallBack = callback;
    }
};

